package com.bdqn.t330.ch12file.config;

import com.aliyun.oss.OSS;
import com.aliyun.oss.OSSClientBuilder;
import org.apache.commons.io.FilenameUtils;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.util.UUID;

public class UploadAliYunUtil {
    //阿里云域名
    public static final String ALI_DOMAIN = "https://yuanxiaoxiang.oss-cn-guangzhou.aliyuncs.com/";

    public static String uploadImage(MultipartFile file) {
        //生成文件名
        String originalFilename = file.getOriginalFilename();
        String ext = "." + FilenameUtils.getExtension(originalFilename);
        String uuid = UUID.randomUUID().toString().replace("-", "");
        String fileName = uuid + ext;
        //地域节点
        String endpoint = "http://oss-cn-guangzhou.aliyuncs.com";
        String accessKeyId = "LTAI5tMPqqWZLASGU6oW5NTC";
        String accesskeySecret = "kVgqaaT6McV2ztTRnvzLstofoCtf2a";
        //oss客户端对象
        OSS ossClient = new OSSClientBuilder().build(endpoint, accessKeyId, accesskeySecret);
        try {
            ossClient.putObject("yuanxiaoxiang", fileName, file.getInputStream());
        } catch (IOException e) {
            e.printStackTrace();
        }
        ossClient.shutdown();
        return ALI_DOMAIN + fileName;
    }
}
